﻿using LxCore;

namespace LxCore
{
    public class Graph : JsIdentifyItem
    {
        public Graph()
        {
            Name = "新建绘图";
        }
        public GraphTypes GraphType
        {
            get => _GraphType;
            set { _GraphType = value; RaisePropertyChanged(nameof(GraphType)); }
        }
        private GraphTypes _GraphType;
        public MeshGraphTypes MeshType
        {
            get => _MeshType;
            set { _MeshType = value; RaisePropertyChanged(nameof(MeshType)); }
        }
        private MeshGraphTypes _MeshType;
        public ScalarFieldTypes ScalarType
        {
            get => _ScalarType;
            set { _ScalarType = value; RaisePropertyChanged(nameof(ScalarType)); }
        }
        private ScalarFieldTypes _ScalarType;
        private VectorFieldTypes _VectorType=VectorFieldTypes.Velocity;
        public VectorFieldTypes VectorType
        {
            get { return _VectorType; }
            set { _VectorType = value; RaisePropertyChanged(nameof(VectorType)); }
        }
        public double ScaleFactor
        {
            get => _ScaleFactor;
            set { _ScaleFactor = value; RaisePropertyChanged(nameof(ScaleFactor)); }
        }
        private double _ScaleFactor = 0.05;

        public void SetSelector(FvBound bound, bool isEnable)
        {
            ResHelper.GetCond(bound).GraphSelector[ID] = isEnable;
        }
        public void SetSelector(FvInner inner, bool isEnable)
        {
            ResHelper.GetCond(inner).GraphSelector[ID] = isEnable;
        }
        public IEnumerable<FvBound> SelectedBound()
        {
            foreach (var z in LxSim.Sim.Conditions.ZoneConds)
            {
                foreach (var f in z.BoundConds)
                {
                    if (f.GraphSelector[ID])
                    {
                        yield return f.Bound;
                    }
                }
            }
        }
        public IEnumerable<FvZone> SelectedZone()
        {
            foreach (var z in LxSim.Sim.Conditions.ZoneConds)
            {
                if (z.InnerCond.GraphSelector[ID])
                {
                    yield return z.Zone;
                }
            }
        }
        public IEnumerable<IIdentity> SelectedMesh()
        {
            foreach (var z in LxSim.Sim.Conditions.ZoneConds)
            {
                if (z.InnerCond.GraphSelector[ID])
                {
                    yield return z.Zone;
                }
                foreach (var f in z.BoundConds)
                {
                    if (f.GraphSelector[ID])
                    {
                        yield return f.Bound;
                    }
                }
            }
        }
        public override void FromJsonKV(string key, IJsonReader jsReader)
        {
            switch (key)
            {
                case nameof(GraphType): GraphType = jsReader.ReadEnum<GraphTypes>(); break;
                case nameof(ScalarType): ScalarType = jsReader.ReadEnum<ScalarFieldTypes>(); break;
                case nameof(VectorType): VectorType = jsReader.ReadEnum<VectorFieldTypes>(); break;
                case nameof(ScaleFactor): ScaleFactor = jsReader.ReadDouble(); break;
                case "Selector": jsReader.ReadGraphSelector(this); break;
                default: base.FromJsonKV(key, jsReader); break;
            }
        }
        public override void ToJsonKV(IJsonWriter jsWriter)
        {
            base.ToJsonKV(jsWriter);
            jsWriter.WriteEnum(nameof(GraphType), GraphType);
            switch (GraphType)
            {
                case GraphTypes.等值图:jsWriter.WriteEnum(nameof(ScalarType), ScalarType);break;
                case GraphTypes.矢量图:jsWriter.WriteEnum(nameof(VectorType), VectorType);jsWriter.WriteDouble(nameof(ScaleFactor), ScaleFactor); break;
            }
            jsWriter.WriteGraphSelector("Selector", this.ID);
        }
    }
    public enum MeshGraphTypes
    {
        solid,
        wire,
        outline,
        solidwire,
        solidoutline,
    }
    public enum GraphTypes
    {
        网格图,
        等值图,
        矢量图,
        迹线图
    }
}
